package com.tianji.remark.service.impl;

import com.tianji.api.dto.trade.OrderBasicDTO;
import com.tianji.common.autoconfigure.mq.RabbitMqHelper;
import com.tianji.common.constants.MqConstants;
import com.tianji.common.utils.StringUtils;
import com.tianji.common.utils.UserContext;
import com.tianji.remark.domain.dto.LikeRecordFormDTO;
import com.tianji.remark.domain.dto.LikedTimesDTO;
import com.tianji.remark.domain.po.LikedRecord;
import com.tianji.remark.mapper.LikedRecordMapper;
import com.tianji.remark.service.ILikedRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 点赞记录表 服务实现类
 * </p>
 *
 * @author author
 * @since 2025-01-26
 */
//@Service
@RequiredArgsConstructor
@Slf4j
public abstract class LikedRecordServiceImpl extends ServiceImpl<LikedRecordMapper, LikedRecord> implements ILikedRecordService {

    private final RabbitMqHelper rabbitMqHelper;
    /**
     *
     * @param dto
     */
    @Override
    public void addLikeRecord(LikeRecordFormDTO dto) {
        //1.获取当前登录用户
        Long userId = UserContext.getUser();

        //2.判断是否点赞 dto.liked 为true是点赞
        boolean flag = dto.getLiked() ? liked(dto,userId) : unliked(dto,userId);
        if (!flag){//说明点或赞或取消赞
            return;
        }
        //3.统计该业务id下的总点赞数
        Integer totalLikesNum = this.lambdaQuery()
                .eq(LikedRecord::getBizId, dto.getBizId())
                .count();

        //4.发生消息到mq
        String routingKey = StringUtils.format(MqConstants.Key.LIKED_TIMES_KEY_TEMPLATE, dto.getBizType());
        LikedTimesDTO msg = LikedTimesDTO.of(dto.getBizId(), totalLikesNum);

        log.debug("发送点赞消息,消息内容{}",msg);
        rabbitMqHelper.send(
                MqConstants.Exchange.LIKE_RECORD_EXCHANGE,
                routingKey,
                msg);
    }

    //取消赞
    private boolean unliked(LikeRecordFormDTO dto,Long userId) {
        LikedRecord record = this.lambdaQuery()
                .eq(LikedRecord::getUserId, userId)
                .eq(LikedRecord::getBizId, dto.getBizId())
                .one();
        if (record == null){
            //说明之前没点过赞
            return false;
        }
        //删除点赞记录
        boolean result = this.removeById(record.getId());
        return result;
    }

    //点赞
    private boolean liked(LikeRecordFormDTO dto,Long userId) {
        LikedRecord record = this.lambdaQuery()
                .eq(LikedRecord::getUserId, userId)
                .eq(LikedRecord::getBizId, dto.getBizId())
                .one();
        if (record != null){
            //说明之前点过赞
            return false;
        }
        //保存点赞记录到表
        LikedRecord likedRecord = new LikedRecord();
        likedRecord.setUserId(userId);
        likedRecord.setBizId(dto.getBizId());
        likedRecord.setBizType(dto.getBizType());
        boolean result = this.save(likedRecord);
        return result;
    }

    /**
     * 批量查询点赞状态
     * @param bizIds
     * @return
     */
    @Override
    public Set<Long> getLikesStatusByBizIds(List<Long> bizIds) {
        //1.获取用户id
        Long userId = UserContext.getUser();

        //2.查点赞记录表
        List<LikedRecord> list = lambdaQuery()
                .in(LikedRecord::getBizId, bizIds)
                .eq(LikedRecord::getUserId, userId)
                .list();

        //3.将查询到的bizid转集合返回
        return list.stream().map(LikedRecord::getBizId).collect(Collectors.toSet());
    }


}
